package com.scaffold.photo.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.photo.entity.Album;
import com.scaffold.photo.service.AlbumService;

/**
 * 相册操作接口(json)
 * @author wangjg
 */
@Controller
public class AlbumJsonAction extends BasicBizAction{
	@Autowired
	private AlbumService albumService;

	/**
	 * 相册列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/album/list.json")
	public String list(String name, String keyword){
		Page page = this.getPage(20);
		List<Album> list = albumService.pageQuery(page, name, keyword);
		return this.getPageResult(page, list);
	}

	/**
	 * 获取相册
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/album/get.json")
	public String get(Long id){
		Album album = albumService.getAlbum(id);
		this.checkOwner(album);
		return this.getSuccessResult(album);
	}

	/**
	 * 保存相册
	 */
	@RequestMapping("/album/save.json")
	public String save(Long id){
		if(id==null){
			Album album = new Album();
			this.bindParams(album);
			this.setOwner(album);
			this.albumService.save(album);
		}else{
			Album album = this.albumService.getAlbum(id);
			this.checkOwner(album);
			this.bindParams(album);
			this.albumService.update(album);
		}
		return this.getSuccessResult();
	}

	/**
	 * 启用/禁用相册
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/album/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		Album album = albumService.getAlbum(id);
		this.checkOwner(album);
		this.albumService.update(album);
		return this.getSuccessResult(enabled);
	}

	/**
	 * 删除相册
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/album/delete.json")
	public String delete(Long id){
		Album album = albumService.getAlbum(id);
		this.checkOwner(album);
		this.albumService.delete(album);
		return this.getSuccessResult();
	}

	/**
	 * 批量删除相册
	 * @param idseq 编号序列(以‘,’号隔开)
	 * @return
	 */
	@RequestMapping("/album/batchDelete.json")
	public String batchDelete(String idseq){
		this.albumService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	/**
	 * 获取相册列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/album/select.json")
	public String select(String name, String keyword){
		List<Album> list = albumService.pageQuery(null, name, keyword);
		return this.getSuccessResult(list);
	}


}
